



class Node(object):
    '''
    初始化链表
    '''
    def __init__(self, item):
        self.item = item
        self.next = None
        self.prev = None


class DLinklist(object):
    '''
    双向链表
    '''
    def __init__(self):
        self._head = None

    def is_empty(self):
        '''
        判断链表是否为空
        :return:
        '''
        return self._head == None

    def length(self):
        '''
        返回链表的长度
        :return:
        '''
        cur = self._head
        count = 0
        while cur != None:
            count += 1
            cur = cur.next
        return count

    def travel(self):

        '''
        遍历链表
        :return:
        '''
        cur = self._head
        while cur != None:
            print(cur.item)
            cur = cur.next
        print("")

    def add(self, item):
        '''
        头部插入元素 从开始往里面插入元素
        :param item:
        :return:
        '''
        node = Node(item)
        if self.is_empty():
        #  如果是空列表。将—head指向node
            self._head = node
        else:
            node.next = self._head
            self._head.prev = node
            self._head = node



    def append(self, item):
        '''
        append 尾部插入
        :param item:
        :return:
        '''
        node = Node(item)
        if self.is_empty():
            self._head = node
        else:
            cur = self._head
            while cur.next != None:
                cur = cur.next
            cur.next = node
            node.prev = cur



    def search(self, item):
        """查找元素是否存在"""
        cur = self._head
        while cur != None:
            if cur.item == item:
                return True
            cur = cur.next
        return False

    def insert(self, pos, item):
        '''
        在指定位置添加节点
        :param self:
        :param pos:
        :param item:
        :return:
        '''
        if pos <= 0:
            self.add(item)
        elif pos > (self.length()-1):
            self.append(item)
        else:
            node = Node(item)
            cur = self._head
            count = 0
            #  移动到指定位置前的一个位置
            while count < (pos-1):
                count += 1
                cur = cur.next
                #  将node 的prev只想cur
                node.next = cur.next
                #   将cur 的下一个节点的prev只想node
                cur.next.prev = node
                #  将cur的next只想node
                cur.next = node
    def remove(self, item):
        '''
        删除元素
        :param item:
        :return:
        '''
        if self.is_empty():
            return
        else:
            cur = self._head
            if cur.item == item:
                #   如果首节点的元素既是要删除的元素
                if cur.next == None:
                    #   如果链表只有这一个节点
                    self._head = None
                else:
                    #     将第二个节点的prev设置为None
                    cur.next.prev = None
                    #   将_head只想第二个节点
                    self._head = cur.next
                return

            while cur != None:
                if cur.item == item:
                    #        将cur的前一个节点的next指向cur的后一个节点
                    cur.prev.next = cur.next
#                     将cur的后一个节点的prev指向cur的前一个节点
                    cur.next.prev = cur.prev
                    break
                cur = cur.next


if __name__ == '__main__':

    l = DLinklist()
    l.add(1)

    l.add(2)
    l.add(8)
    l.add(10)

    l.append(6)

    l.append(9)



    print("length:",l.length())
    print(l.travel())

    l.remove(6)
    '''
    遍历直接print
    '''
    print(l.travel())
    l.remove(10)
    print(l.travel())
